package com.javatiaocao.myblog.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.javatiaocao.myblog.constant.CodeType;
import com.javatiaocao.myblog.mapper.FriendLinkMapper;
import com.javatiaocao.myblog.model.FriendLink;
import com.javatiaocao.myblog.service.FriendLinkService;
import com.javatiaocao.myblog.utils.DataMap;
import com.javatiaocao.myblog.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class FriendLinkServiceImpl implements FriendLinkService {
    @Autowired
    FriendLinkMapper friendLinkMapper;

    @Override
    public DataMap getFriendLink() {
        //目前友链数：data['data'].length
        //友链信息 data['data'][i]['id'] data['data'][i]['blogger'] data['data'][i]['url']
        List<FriendLink> friendLinkList = friendLinkMapper.getFriendLink();
        if (friendLinkList == null || StringUtil.BLANK.equals(friendLinkList)){
            return DataMap.fail(CodeType.COMMENT_BLANK);
        }
//        JSONArray friendLinkArray = new JSONArray();
//        for (FriendLink friendLink : friendLinkList) {
//            //友联数据处理
//            JSONObject friendLinkObject = new JSONObject();
//            friendLinkObject.put("id",friendLink.getId());
//            friendLinkObject.put("blogger",friendLink.getBlogger());
//            friendLinkObject.put("url",friendLink.getUrl());
//            friendLinkArray.add(friendLinkObject);
//        }
        return DataMap.success().setData(friendLinkList);
    }

    @Override
    public DataMap updateFriendLink(FriendLink friendLink) {
        if(friendLink == null){
            return DataMap.fail(CodeType.COMMENT_BLANK);
        }

        Boolean isExistFriendLink = friendLinkMapper.isExistFriendLink(friendLink.getUrl());
        if (isExistFriendLink ){
            return DataMap.fail(CodeType.FRIEND_LINK_EXIST);
        }



        friendLinkMapper.updateFriendLink(friendLink);
        return DataMap.success(CodeType.UPDATE_FRIEND_LINK_SUCCESS);
    }

    @Override
    public DataMap addFriendLink(FriendLink friendLink) {
        if(friendLink == null){
            return DataMap.fail(CodeType.COMMENT_BLANK);
        }
        Boolean isExistFriendLink = friendLinkMapper.isExistFriendLink(friendLink.getUrl());
        if (isExistFriendLink){
            return DataMap.fail(CodeType.FRIEND_LINK_EXIST);
        }
       friendLinkMapper.addFriendLink(friendLink);
        return DataMap.success(CodeType.ADD_FRIEND_LINK_SUCCESS);
    }

    @Override
    public DataMap deleteFriendLink(String id) {
        if (StringUtil.BLANK.equals(id) && id == null){
            return DataMap.fail(CodeType.COMMENT_BLANK);
        }
        friendLinkMapper.deleteFriendLink(id);

        return DataMap.success(CodeType.DELETE_FRIEND_LINK_SUCCESS);
    }

    @Override
    public DataMap getFriendLinkInfo() {
        //数据封装
        ArrayList<HashMap> returnHashMap = new ArrayList<>();

        //获取所有友联
        List<FriendLink> friendLink = friendLinkMapper.getFriendLink();
        if (friendLink == null || friendLink.size() == 0){
            return DataMap.fail(CodeType.COMMENT_BLANK);
        }
        for (FriendLink link : friendLink) {
            HashMap<String, Object> stringObjectHashMap = new HashMap<>();
            stringObjectHashMap.put("url", link.getUrl());
            stringObjectHashMap.put("blogger", link.getBlogger());
            returnHashMap.add(stringObjectHashMap);
        }

        return DataMap.success().setData(returnHashMap);
    }


}
